package com.fh.fruit.controller.activity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.fruit.service.activity.ActivityGoldBowlManager;
import com.fh.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.ObjectExcelView;
import com.fh.util.PageData;

@Controller
@RequestMapping(value = "/goldBowl")
public class ActivityGoldBowlController extends BaseController {
	String menuUrl = "goldBowl/listActivity.do"; // 地址(权限用)

	@Resource(name = "goldBowlService")
	private ActivityGoldBowlManager goldBowlService;
	
	/**金饭碗用户邀请统计
	 * @param page
	 * @param httpSession
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value="/listActivity")
	public ModelAndView listActivity(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String endTime=pd.getString("endTime");
		page.setPd(pd);
		List<Map<String,Object>> activityList = goldBowlService.getListActivity(page);
		pd.put("num", page.getTotalResult());
		pd.put("endTime", endTime);
		mv.setViewName("farm/count/golen_bowl_list");
		mv.addObject("activityList", activityList);
		mv.addObject("pd", pd); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	
	/**导出用户注册信息到EXCEL
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try{
			if(Jurisdiction.buttonJurisdiction(menuUrl, "cha")){
				
				Map<String,Object> dataMap = new HashMap<String,Object>();
				List<String> titles = new ArrayList<String>();
				titles.add("UID"); 		//1
				titles.add("昵称");  		//2
				titles.add("手机号");			//3
				titles.add("邀请注册人数");			//4
				titles.add("购买金饭碗人数");			//5
				titles.add("邀请购买金饭碗总金额");			//6
				titles.add("注册日期");		//7
				dataMap.put("titles", titles);
				page.setShowCount(Integer.parseInt(pd.getString("showCount")));
				page.setCurrentPage(Integer.parseInt(pd.getString("pageNum")));
				page.setPd(pd);
				List<Map<String,Object>> activityList = goldBowlService.getListActivity(page);
				List<PageData> varList = new ArrayList<PageData>();
				for(int i=0;i<activityList.size();i++){
					PageData vpd = new PageData();
					String price1="";
					BigDecimal price=(BigDecimal)activityList.get(i).get("price");
					if(price == null ||"".equals(price)){
						price1="未消费";
					} else {
						price1=price.toString()+ " 元";
					}
					vpd.put("var1", (Integer)activityList.get(i).get("id"));		//1
					vpd.put("var2", (String)activityList.get(i).get("petName"));		//2
					vpd.put("var3", (String)activityList.get(i).get("phone"));			//3
					vpd.put("var4", (Long)activityList.get(i).get("num")+" 人");	//4
					vpd.put("var5", (Long)activityList.get(i).get("downNum")+" 人");		//5
					vpd.put("var6", price1);		//6
					vpd.put("var7", (String)activityList.get(i).get("creation_date"));	//7
					varList.add(vpd);
				}
				dataMap.put("varList", varList);
				ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
				mv = new ModelAndView(erv,dataMap);
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**金饭碗售出商品统计
	 * @param page
	 * @param httpSession
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value="/workOff")
	public ModelAndView workOff(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String endTime=pd.getString("endTime");
		page.setPd(pd);
		List<Map<String,Object>> workOff = goldBowlService.getListWorkOff(page);
		pd.put("num", page.getTotalResult());
		pd.put("endTime", endTime);
		mv.setViewName("farm/count/work_off_list");
		mv.addObject("workOff", workOff);
		mv.addObject("pd", pd); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	
	/**导出用户注册信息到EXCEL
	 * @return
	 */
	@RequestMapping(value="/workExcel")
	public ModelAndView workExcel(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try{
			if(Jurisdiction.buttonJurisdiction(menuUrl, "cha")){
				
				Map<String,Object> dataMap = new HashMap<String,Object>();
				List<String> titles = new ArrayList<String>();
				titles.add("ID"); 		//1
				titles.add("期数");  		//2
				titles.add("商品名");			//3
				titles.add("下单时间");			//4
				titles.add("金额 (元)");	//5
				titles.add("数量");//6
				titles.add("用户昵称");			//7
				titles.add("手机号");		//8
				dataMap.put("titles", titles);
				page.setShowCount(Integer.parseInt(pd.getString("showCount")));
				page.setCurrentPage(Integer.parseInt(pd.getString("pageNum")));
				page.setPd(pd);
				List<Map<String,Object>> workOff = goldBowlService.getListWorkOff(page);
				List<PageData> varList = new ArrayList<PageData>();
				for(int i=0;i<workOff.size();i++){
					BigDecimal bd =(BigDecimal)workOff.get(i).get("real_price");
					if(bd != null){
						bd=bd.setScale(2,BigDecimal.ROUND_HALF_UP);
					} 
					PageData vpd = new PageData();
					vpd.put("var1", (Long)workOff.get(i).get("id"));		//1
					vpd.put("var2", (String)workOff.get(i).get("time")+ " 期");		//2
					vpd.put("var3", (String)workOff.get(i).get("title"));			//3
					vpd.put("var4", (String)workOff.get(i).get("create_time"));	//4
					vpd.put("var5", bd);		//5
					vpd.put("var6", (Integer)workOff.get(i).get("number")+ " 个");		//6
					vpd.put("var7", (String)workOff.get(i).get("petName"));		//7
					vpd.put("var8", (String)workOff.get(i).get("phone"));	//8
					varList.add(vpd);
				}
				dataMap.put("varList", varList);
				ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
				mv = new ModelAndView(erv,dataMap);
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**金饭碗售出商品统计
	 * @param page
	 * @param httpSession
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value="/wholeDetails")
	public ModelAndView wholeDetails(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String times=pd.getString("times");
		if(times != null && !"".equals(times)){
			if("1".equals(times)){
				pd.put("startTime", DateUtil.getDay());
				pd.put("endTime", DateUtil.getTime());
			} else if("2".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-1"));
				pd.put("endTime", DateUtil.getAfterDayDates("-1"));
			} else if("3".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-2"));
				pd.put("endTime", DateUtil.getAfterDayDates("-2"));
			} else if("4".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-7"));
				pd.put("endTime", DateUtil.getTime());
			} else if("5".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-30"));
				pd.put("endTime", DateUtil.getTime());
			}
		}
		String endTime=pd.getString("endTime");
		if(endTime != null && !"".equals(endTime)){
			pd.put("endTime", DateUtil.getSpecifiedDayBefore(pd.getString("endTime")));
		}
		Map<String,Object> info = goldBowlService.getGoldBowInfo(pd);
		List<Map<String,Object>> goodsInfo = goldBowlService.getGoldBowGoodsInfo(pd);
		Map<String,Object> number = goldBowlService.getGoldBowNum(pd);
		pd.put("endTime", endTime);
		mv.addObject("info", info);
		mv.addObject("goodsInfo", goodsInfo);
		mv.addObject("number", number);
		mv.setViewName("farm/count/jfw-overall-statistics");
		mv.addObject("pd", pd); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	/**分润用户邀请统计
	 * @param page
	 * @param httpSession
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value="/profitActivity")
	public ModelAndView profitActivity(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String endTime=pd.getString("endTime");
		page.setPd(pd);
		List<Map<String,Object>> activityList = goldBowlService.getProfitActivity(page);
		pd.put("endTime", endTime);
		pd.put("num", page.getTotalResult());
		mv.setViewName("farm/count/profit_activity_list");
		mv.addObject("activityList", activityList);
		mv.addObject("pd", pd); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	/**分润活动整体统计
	 * @param page
	 * @param httpSession
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value="/profitDetails")
	public ModelAndView profitDetails(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String times=pd.getString("times");
		if(times != null && !"".equals(times)){
			if("1".equals(times)){
				pd.put("startTime", DateUtil.getDay());
				pd.put("endTime", DateUtil.getTime());
			} else if("2".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-1"));
				pd.put("endTime", DateUtil.getAfterDayDates("-1"));
			} else if("3".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-2"));
				pd.put("endTime", DateUtil.getAfterDayDates("-2"));
			} else if("4".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-7"));
				pd.put("endTime", DateUtil.getTime());
			} else if("5".equals(times)){
				pd.put("startTime", DateUtil.getAfterDayDates("-30"));
				pd.put("endTime", DateUtil.getTime());
			}
		}
		String endTime=pd.getString("endTime");
		if(endTime != null && !"".equals(endTime)){
			pd.put("endTime", DateUtil.getSpecifiedDayBefore(pd.getString("endTime")));
		}
		Map<String,Object> info = goldBowlService.getProfitDetails(pd);
		pd.put("endTime", endTime);
		mv.addObject("info", info);
		mv.setViewName("farm/count/activity-overall-statistics");
		mv.addObject("pd", pd); 
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	/**导出被邀请用户注册信息到EXCEL
	 * @return
	 */
	@RequestMapping(value="/profitExcel")
	public ModelAndView profitExcel(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("UID"); 		//1
			titles.add("昵称");  		//2
			titles.add("手机号");			//3
			titles.add("购买商品数");			//4
			titles.add("消费金额 (元)");	//5
			titles.add("上级用户昵称");//6
			titles.add("上级用户手机");			//7
			titles.add("注册日期");		//8
			dataMap.put("titles", titles);
			page.setShowCount(Integer.parseInt(pd.getString("showCount")));
			page.setCurrentPage(Integer.parseInt(pd.getString("pageNum")));
			page.setPd(pd);
			List<Map<String,Object>> activityList = goldBowlService.getProfitActivity(page);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<activityList.size();i++){
				BigDecimal bd =(BigDecimal)activityList.get(i).get("price");
				if(bd != null){
					bd=bd.setScale(2,BigDecimal.ROUND_HALF_UP);
				} 
				PageData vpd = new PageData();
				vpd.put("var1", (Integer)activityList.get(i).get("id"));		//1
				vpd.put("var2", (String)activityList.get(i).get("petName"));		//2
				vpd.put("var3", (String)activityList.get(i).get("phone"));			//3
				vpd.put("var4", (Long)activityList.get(i).get("goodsNum"));	//4
				vpd.put("var5", bd);		//5
				vpd.put("var6", (String)activityList.get(i).get("pName"));		//6
				vpd.put("var7", (String)activityList.get(i).get("pPhone"));		//7
				vpd.put("var8", (String)activityList.get(i).get("creation_date"));	//8
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
}
